--
-- Author: shunguo.chen
-- Date: 2018-03-19 14:19:37
--
-- ChessReadyView 谜棋战前准备

local ChessHeadIconRender = import(".renders.ChessHeadIconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ChessReadyScene

function ClassRef:init()
	display.nodeRemoveForNotHit(self._ui.Panel, function() 
			executeFunc(self._onCloseView, self)
		end)

	local con = self._ui.Panel_content

	local roleRenders = {0,0,0}
	for i=1,3 do
		local node = con:getChildByName("Panel_" .. i)
		local render = ChessHeadIconRender.create(node)
		roleRenders[i] = render
	end
	self._roleRenders = roleRenders

	local skillNodes = {0,0,0,0}
	for i=1,4 do
		local node = con:getChildByName("skill_" .. i)
		skillNodes[i] = node
	end
	self._skillNodes = skillNodes

	self._descText = con:getChildByName("text")
end

-- chessPlayer = ChessPlayer
-- newRoleId = 新获得的角色id
function ClassRef:refresh(player, newRoleId)
	self._descText:setString(L("starChessTips_1"))

	local roles = player:getRoles()
	for i,render in ipairs(self._roleRenders) do
		local role = roles[i]
		render:setData(role)
		if role and role.id == newRoleId then
			render:setIsNew(true)
		else
			render:setIsNew(false)
		end
	end

	local skills = player:getSkills()
	for i,node in ipairs(self._skillNodes) do
		local skill = skills[i]
		node:setVisible(skill ~= nil)
		if skill then
			node:getChildByName("icon"):loadTexture(skill:getIcon())
			node:addTouchEventListener(function(sender, eventType)
				if eventType == ccui.TouchEventType.began then
					UIHandler:hideFloatPanel()
					local boundingBox = sender:getBoundingBox()
					local worldPos = sender:convertToWorldSpace(cc.p(0, 0))
					boundingBox.x = worldPos.x
					boundingBox.y = worldPos.y
					local params = {
						tipsType = Enums.TipsType.classSkill,
						dataTemp = skill,
						boundingBox = boundingBox
					}
					app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=params})
				elseif eventType == ccui.TouchEventType.canceled or 
					eventType == ccui.TouchEventType.ended then
					UIHandler:hideFloatPanel()
				end
			end)
		end
	end

	if newRoleId then
		self:performWithDelay(function()
				executeFunc(self._onCloseView, self)
			end, 2)
	end
end

return ClassRef